package com.kgc.ssm.service.impl;

import com.kgc.ssm.entity.*;
import com.kgc.ssm.entity.dto.OrderDTO;
import com.kgc.ssm.entity.dto.Result;
import com.kgc.ssm.mapper.IdMappingMapper;
import com.kgc.ssm.mapper.MaterialMapper;
import com.kgc.ssm.mapper.OrdersMapper;
import com.kgc.ssm.service.OrderService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private IdMappingMapper idMappingMapper;
    @Override
    public List<Material> findMaterial(String MaterialNum, int pageNum, int pageSize){
        MaterialExample materialExample = new MaterialExample();
        materialExample.createCriteria().andMaterialNumLike("%"+MaterialNum+"%");
        int offset=(pageNum-1)*pageSize;
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<Material> materials = materialMapper.selectByExampleWithRowbounds(materialExample, rowBounds);
        return materials;
    }

    @Override
    public long countMaterial(String MaterialNum) {
        MaterialExample materialExample = new MaterialExample();
        materialExample.createCriteria().andMaterialNumLike("%"+MaterialNum+"%");
        long count = materialMapper.countByExample(materialExample);
        System.out.println(count);
        return count;
    }

    @Override
    public List<Material> findMaterialByIds(List MaterialIds, int pageNum, int pageSize) {
        MaterialExample materialExample = new MaterialExample();
        materialExample.createCriteria().andIdIn(MaterialIds);
        int offset=(pageNum-1)*pageSize;
        RowBounds rowBounds = new RowBounds(offset,pageSize);
        List<Material> materials = materialMapper.selectByExampleWithRowbounds(materialExample, rowBounds);
        return materials;
    }

    @Override
    public Long countFindMaterialByIds(List MaterialIds) {
        MaterialExample materialExample = new MaterialExample();
        materialExample.createCriteria().andIdIn(MaterialIds);
         long l = materialMapper.countByExample(materialExample);
        return l;
    }

    @Override
    public int addOrder(Orders order) {
        int i = ordersMapper.insertSelective(order);
        long id = ordersMapper.selectLastId();
        System.out.println(id);
        IdMapping idMapping = new IdMapping();
        idMapping.setOrderId(id);
        idMapping.setStatus("C001-10");
        idMappingMapper.insertSelective(idMapping);
        return i;
    }

    @Override
    public Result<List<OrderDTO>> getOrders(String materialCode, String materialName, String status, int pageNum, int pageSize) {
        if ("未确定".equals(status)) {
            status = "C001-10";
        } else if ("确定".equals(status)) {
            status = "C001-20";
        } else {
            status = null;
        }

        int offset = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);
        List<Map<String, Object>> results = ordersMapper.findOrdersByConditions(materialCode, materialName, status, rowBounds);
        List<OrderDTO> orderDTOs = mapToDTO(results);
        Long totalRecords = ordersMapper.countOrdersByConditions(materialCode, materialName, status);

        return new Result<>("查询成功", 200, orderDTOs, totalRecords);
    }
    public void updateOrderStatus(List<Integer> orderIds, String status) {
        if (orderIds == null || orderIds.isEmpty()) {
            throw new IllegalArgumentException("订单ID列表不能为空");
        }
        ordersMapper.updateOrderStatus(orderIds, status);
    }

    private List<OrderDTO> mapToDTO(List<Map<String, Object>> results) {
        List<OrderDTO> orderDTOs = new ArrayList<>();
        for (Map<String, Object> result : results) {
            OrderDTO dto = new OrderDTO();
            System.out.println(result);
            dto.setOrderId((Integer) result.get("id"));
            dto.setMaterialCode((String) result.get("MATERIAL_CODE"));
            dto.setMaterialName((String) result.get("MATERIAL_NAME"));
            dto.setAmount((String) result.get("AMOUNT"));
            dto.setStatus((String) result.get("status"));
            orderDTOs.add(dto);
        }
        return orderDTOs;
    }
}
